package cn.edu.siso.imhere;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class HomeActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        RelativeLayout topBar = (RelativeLayout) findViewById(R.id.top_bar);
        ImageView icon = (ImageView) topBar.findViewById(R.id.top_bar_left_icon);
        icon.setImageResource(R.mipmap.ic_launcher);

        TextView title = (TextView) topBar.findViewById(R.id.top_bar_title);
        title.setText(R.string.title_activity_home);

        // 获取颜色列表布局
        LinearLayout hereColorLayout = (LinearLayout) findViewById(R.id.here_color_layout);

        // 为每个颜色添加点击事件
        for (int i = 1; i < hereColorLayout.getChildCount(); i++) {
            final int index = i - 1; // 获取点击对象的索引
            hereColorLayout.getChildAt(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putInt(getResources().getString(R.string.color_bundle_key),
                            index);
                    Intent intent = new Intent(HomeActivity.this, HereActivity.class);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
